<!--#include file="./header.inc"-->
<%

select case aq
	'genre
	case "save" 	: 	call save
	case "edit" 	: 	call edit
	case "delete" 	: 	call delete
	case "status" 	: 	call status
	case else 		: 	call index
end select

call db.close()

function index()
	genreType = app.int(request("genreType"))
	sql = "select genreID,genreName,genreShortName,genreUnit,genreViewID,isStop from cj_genre "
	if genreType > 0 then sql = sql & " where genreType=" & genreType
	sql = sql & " Order By genreID desc"
	temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/genre.index.html"--><%
end function

function edit()
	data = array("", "", "", 0, 0, 0)
	if iid > 0 then
		set rs = db.exec("select genreName,genreShortName,genreUnit,genreType,genreViewID,isStop from cj_genre where genreID=" & iid, 1)
		if rs.eof then admin.message "指定的大类ID没有找到!"
		data = rs
		set rs = nothing
	end if
	%><!--#include file="./view/genre.edit.html"--><%
end function

function save()
	genreName = admin.filter(request.form("genreName"))
	genreShortName = admin.filter(request.form("genreShortName"))
	genreUnit = admin.filter(request.form("genreUnit"))
	genreViewID = app.int(request.form("genreViewID"))
	if genreName = "" or len(genreName) > 50 then admin.message "大类名称不能为空或者不能超过50字！"
	if iid = 0 then
		genreType = app.int(request.form("genreType"))
		if genreType < 1 and genreType > 3 then admin.message "大类类型不存在！"
	end if
	sql = "select * from cj_genre"
	if iid > 0 then
		if not isArray(app.getGenreInfo(iid)) then admin.message "指定的大类ID不存在！"
		sql = sql & " where genreID=" & iid
	end if
	if admin.queryTable("id", "view where id=" & genreViewID) = false then
		admin.message "选择的首页模板ID不存在！"
	end if
	set rs = db.exec(sql, 3)
	if iid = 0 then
		rs.AddNew
		rs("genreType") = genreType
	else
		if rs.eof then admin.message "指定的大类ID不存在！"
	end if
	rs("genreName") = genreName
	rs("genreShortName") = genreShortName
	rs("genreUnit") = genreUnit
	rs("genreViewID") = genreViewID
	rs("isStop") = admin.isCheck(request.form("isStop"))
	rs.update
	rs.close : set rs = nothing
	admin.updateWebAllData()
	if iid > 0 then
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function delete()
    if iid = 0 then app.json "请选择ID！", 1
    sql = "select count(genreID),"
    sql = sql & "(select count(genreID) from cj_product where genreID=" & iid & "),"
    sql = sql & "(select count(genreID) from cj_news where genreID=" & iid & "),"
    sql = sql & "(select count(genreID) from cj_book where genreID=" & iid & ")"
    sql = sql & " from cj_class where genreID=" & iid
    set rs = db.exec(sql, 1)
    if rs(0) > 0 or rs(1) > 0 or rs(2) > 0 or rs(3) > 0 then
        app.json "正在使用的大类不可以删除！",1
    end if
	call db.exec("Delete from cj_genre where genreID=" & iid, 1)
	admin.updateWebAllData()
	app.json "删除成功！", 0
end function

function status()
	dim isStop, msg
    if iid = 0 then app.json "请选择ID！", 1
    isStop = app.cj_bool(request("isStop"))
    msg = "启用"
    if isStop then
        msg = "禁用"
    end if
	call db.exec("update cj_genre set isStop=" & isStop & " where genreID=" & iid, 1)
	admin.updateWebAllData()
	app.json msg & "成功！", 0
end function

%>